package ocr.bankcard;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import com.yyfax.common.CLog;
import com.yylending.las.R;

/**
 * Created by laixj on 2017/7/3.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */

public class BankCardScanView extends View {
	private final static String TAG = "CustomView";

	//扫描框
	private Rect mGuideRect;
	//返回 文字区域
	private Rect mBackRect;
	private int view_width, view_height;
	private static final int BUTTON_TOUCH_TOLERANCE = 30;
	private String mTopText, mBottomText, mBackText;

	private int mLineWidth = 4;
	private int mRoundWidth = 38;
	private int mFrameColor;
	private int mTopTextSize, mBottomTextSize, mBackTextSize;

	private int mMarginLeft, mMarginRight, mMarginTop, mMarginBottom, mImageWidth, mImageHeight;

	private Drawable mHintImage;
	private OnBackListener mListener;
	private Paint mRectPaint = new Paint();
	private Paint mFramePaint = new Paint();
	private Paint mTopTextPaint = new Paint();
	private Paint mBottomTextPaint = new Paint();
	private Paint mBackTextPaint = new Paint();
	private Paint mHintImagePaint = new Paint();

	public interface OnBackListener {
		void goBack();
	}

	public BankCardScanView(Context context) {
		super(context);
		initView(context);
		initPaint();
	}


	public BankCardScanView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
		initPaint();
	}

	public BankCardScanView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView(context);
		initPaint();
	}

	private void initView(Context context) {
		mTopText = getResources().getString(R.string.please_put_bank_card_align);
		mBottomText = getResources().getString(R.string.move_to_remove_facula);
		mBackText = getResources().getString(R.string.back);
		initSize(context);
		mMarginLeft = (int) (getResources().getDisplayMetrics().density * 45);
		mMarginRight = (int) (getResources().getDisplayMetrics().density * 45);
		mMarginTop = (int) (getResources().getDisplayMetrics().density * 104);
		mMarginBottom = (int) (getResources().getDisplayMetrics().density * 104);
		mImageWidth = (int) (getResources().getDisplayMetrics().density * 68);
		mImageHeight = (int) (getResources().getDisplayMetrics().density * 68 / 21 * 22);
		Resources resources = getResources();
		mFrameColor = resources.getColor(R.color.color_4b7cf9);
		mLineWidth = (int) (getResources().getDisplayMetrics().density * 2);
		mRoundWidth = (int) (getResources().getDisplayMetrics().density * 19);

		mTopTextSize = (int) (getResources().getDisplayMetrics().density * 14.5);
		mBottomTextSize = (int) (getResources().getDisplayMetrics().density * 14.5);
		mBackTextSize = (int) (getResources().getDisplayMetrics().density * 15);

		mHintImage = getResources().getDrawable(R.drawable.bank_card_forground);

		//android:layout_marginBottom="30dp" android:layout_marginLeft="65dp" android:layout_marginRight="95dp" android:layout_marginTop="30dp"
		mGuideRect = new Rect(mMarginLeft, mMarginTop, view_width - mMarginRight, view_height - mMarginBottom);
	}

	private void initPaint(){
		mRectPaint.setColor(Color.TRANSPARENT);

		mFramePaint.setColor(mFrameColor);

		mTopTextPaint.setColor(Color.WHITE);
		mTopTextPaint.setAntiAlias(true);
		mTopTextPaint.setTextAlign(Paint.Align.CENTER);
		mTopTextPaint.setTextSize(mTopTextSize);

		mBottomTextPaint.setColor(Color.WHITE);
		mBottomTextPaint.setAntiAlias(true);
		mBottomTextPaint.setTextAlign(Paint.Align.CENTER);
		mBottomTextPaint.setTextSize(mBottomTextSize);

		mBackTextPaint.setColor(Color.WHITE);
		mBackTextPaint.setAntiAlias(true);
		mBackTextPaint.setTextAlign(Paint.Align.CENTER);
		mBackTextPaint.setTextSize(mBackTextSize);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawARGB(153, 0, 0, 0);
		//绘制扫描框
		canvas.save();
		mRectPaint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
		canvas.drawRect(mGuideRect.left + mLineWidth, mGuideRect.top + mLineWidth, mGuideRect.right - mLineWidth, mGuideRect.bottom - mLineWidth, mRectPaint);

		//left=195;right=1635;top=90;bottom=990
		canvas.drawRect(mGuideRect.left, mGuideRect.top, mGuideRect.left + mRoundWidth, mGuideRect.top + mLineWidth, mFramePaint);
		canvas.drawRect(mGuideRect.left, mGuideRect.top, mGuideRect.left + mLineWidth, mGuideRect.top + mRoundWidth, mFramePaint);
		canvas.drawRect(mGuideRect.right - mRoundWidth, mGuideRect.top, mGuideRect.right, mGuideRect.top + mLineWidth, mFramePaint);
		canvas.drawRect(mGuideRect.right - mLineWidth, mGuideRect.top, mGuideRect.right, mGuideRect.top + mRoundWidth, mFramePaint);
		canvas.drawRect(mGuideRect.left, mGuideRect.bottom - mLineWidth, mGuideRect.left + mRoundWidth, mGuideRect.bottom, mFramePaint);
		canvas.drawRect(mGuideRect.left, mGuideRect.bottom - mRoundWidth, mGuideRect.left + mLineWidth, mGuideRect.bottom, mFramePaint);
		canvas.drawRect(mGuideRect.right - mRoundWidth, mGuideRect.bottom - mLineWidth, mGuideRect.right, mGuideRect.bottom, mFramePaint);
		canvas.drawRect(mGuideRect.right - mLineWidth, mGuideRect.bottom - mRoundWidth, mGuideRect.right, mGuideRect.bottom, mFramePaint);

		canvas.restore();

		//绘制扫描线
		/*canvas.save();
		canvas.translate(getWidth() / 2 - (getWidth() - mMarginLeft - mMarginRight) / 6, 0);
		canvas.rotate(90);
		int lft = 0, top = 0, rgt = 0, btm = 0;
		lft = mGuideRect.top + mLineWidth;
		rgt = mGuideRect.bottom - mLineWidth;
		top = 0;
		btm = top + 1;
		Rect laserRect = new Rect(lft, top, rgt, btm);
		Paint mFramePaint = new Paint();
		mFramePaint.setStyle(Paint.Style.FILL);
		mFramePaint.setColor(0xff64a6ff);
		mFramePaint.setStrokeWidth(3f);
		canvas.drawRect(laserRect, mFramePaint);
		canvas.restore();*/

		//绘制提示文字
		canvas.save();
		canvas.translate(getWidth() - mMarginRight / 2 - mTopTextSize / 2, (getHeight() - mMarginBottom - mMarginTop) / 2 + mMarginTop);
		canvas.rotate(90);
		canvas.drawText(mTopText, 0, 0, mTopTextPaint);
		canvas.restore();

		canvas.save();
		canvas.translate(mMarginLeft / 2 - mBottomTextSize / 2, (getHeight() - mMarginBottom - mMarginTop) / 2 + mMarginTop);
		canvas.rotate(90);
		canvas.drawText(mBottomText, 0, 0, mBottomTextPaint);
		canvas.restore();

		canvas.save();
		canvas.translate(getWidth() / 2 - mBackTextSize / 2, getHeight() - mMarginBottom / 2);
		canvas.rotate(90);
		canvas.drawText(mBackText, 0, 0, mBackTextPaint);
		canvas.restore();

		mBackRect = new Rect(getWidth() / 2 - mBackTextSize * 2, getHeight() - mMarginBottom / 2 - mBackTextSize * mBackText.toString().length(),
				getWidth() / 2 + mBackTextSize * 2, getHeight() - mMarginBottom / 2 + mBackTextSize * mBackText.toString().length());
		/*Paint paint4 = new Paint();
		paint4.setColor(Color.WHITE);
		paint4.setStyle(Paint.Style.STROKE);
		canvas.drawRect(mBackRect, paint4);*/

		canvas.save();
		canvas.translate(getWidth() - mMarginRight, getHeight() - mMarginBottom / 2 - mImageWidth / 2);
		canvas.rotate(90);
		Rect mLineRect = new Rect(0, 0, mImageWidth, mImageHeight);
		mHintImage.setBounds(mLineRect);
		mHintImage.draw(canvas);
		canvas.restore();
	}

	private void initSize(Context context) {
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Point size = new Point();
		wm.getDefaultDisplay().getSize(size);
		view_width = size.x;
		view_height = size.y;
	}

	public Rect getGuide() {
		return mGuideRect;
	}

	public OnBackListener getListener() {
		return mListener;
	}

	public void setListener(OnBackListener listener) {
		mListener = listener;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		try {
			int action;
			action = event.getAction() & MotionEvent.ACTION_MASK;
			if (action == MotionEvent.ACTION_DOWN) {
				Point p = new Point((int) event.getX(), (int) event.getY());
				Rect r = rectGivenCenter(p, BUTTON_TOUCH_TOLERANCE, BUTTON_TOUCH_TOLERANCE);
				if (mBackRect != null && Rect.intersects(mBackRect, r)) {
					if (null != mListener)
						mListener.goBack();
				}
			}
		} catch (NullPointerException e) {
			// Un-reproducible NPE reported on device without flashbank where flashbank detected and flashbank
			// button pressed (see https://github.com/paypal/PayPal-Android-SDK/issues/27)
			CLog.d("customer", "NullPointerException caught in onTouchEvent method");
		}
		return false;
	}


	private Rect rectGivenCenter(Point center, int width, int height) {
		return new Rect(center.x - width / 2, center.y - height / 2, center.x + width / 2, center.y + height / 2);
	}

}
